/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TbedItemPropertyPanel.java
 *
 * Created on 10.Şub.2010, 11:48:20
 */
package org.testis.gui.mvc.view.testbed;

import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.controller.testbed.TbedTreeController;
import org.testis.gui.mvc.view.AbstractViewPanel;
import org.testis.orm.testbed.refined.TestBedItem;
import org.testis.utils.tree.TbedMutableTreeNode;
import java.beans.PropertyChangeEvent;
import javax.swing.tree.TreePath;

/**
 *
 * @author selcuk
 */
public class TbedItemPropertyPanel extends javax.swing.JPanel implements AbstractViewPanel {

    private TbedTreeController m_controller;

    /** Creates new form TbedItemPropertyPanel */
    public TbedItemPropertyPanel() {
        m_controller = TestBedControllerList.newInstance().getTbedTreeController();
        m_controller.addView(this);

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        add(jScrollPane1);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                TbedTreeController.SELECTED_NODE_PROPERTY)) {
            TbedMutableTreeNode node = (TbedMutableTreeNode) evt.getNewValue();
            onSelectedNodeChanged(node);
        } else if (evt.getPropertyName().equals(
                TbedTreeController.SELECTION_TREE_PATH_PROPERTY)) {
            TreePath path = (TreePath) evt.getNewValue();
            onSelectionTreePathChanged(path);
        }
    }

    private void onSelectedNodeChanged(TbedMutableTreeNode selectedNode) {
        TestBedItem item = null;
        if (selectedNode != null) {
            if (selectedNode.getUserObject() != null && selectedNode.getUserObject() instanceof TestBedItem) {
                item = (TestBedItem) selectedNode.getUserObject();
            }
        }

        if(item != null) {
            jTextArea1.setText(item.toString(true));
        } else {
            jTextArea1.setText("");
        }
    }

    private void onSelectionTreePathChanged(TreePath path) {

    }
}
